﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Resources;
using System.Collections;
using System.IO;

namespace project_assess
{
    public partial class loginfrm : Office2007Form
    {
       
      
        public bool pass=false;
        DataBase m_db;
        public user us;
        public loginfrm(DataBase db)
        {
            InitializeComponent();
            m_db = db;
            if (File.Exists("project_assess.resources"))
            {
                ResourceReader resources = new ResourceReader("project_assess.resources");
                string check, name, psw;
                check = name = psw = string.Empty;
                foreach (DictionaryEntry d in resources)
                {
                    if (d.Key.Equals("check"))
                    {
                        check = d.Value.ToString();
                    }
                    else if (d.Key.Equals("user"))
                    {
                        name = d.Value.ToString();
                    }
                    else if (d.Key.Equals("password"))
                    {
                        psw = d.Value.ToString();
                    }
                }
                if (check.Equals("1"))
                {
                    checkBoxX.Checked = true;
                    textBoxX1.Text = name;
                    textBoxX2.Text = psw;
                }

                resources.Close();
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string name = textBoxX1.Text;
            string psw = textBoxX2.Text;
            string sql = "Select [密码],[权限] from [users] where [姓名]='" + name + "'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                string psw1 = dt.Rows[0][0].ToString();

                if (psw.Equals(psw1))
                {
                    pass = true;
                    us = new user(name, psw1, Convert.ToInt32(dt.Rows[0][1]));
                }
                else
                {
                    checkBoxX.Checked = false;
                    pass = false;
                }
            }
            else
            {
                checkBoxX.Checked = false;
                pass = false;
            }
   
            this.Hide();
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        private void textBoxX1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                buttonX1_Click(buttonX1, new EventArgs());
            }
        }

        private void checkBoxX_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxX.Checked == true)
            {
                if (textBoxX1.Text != "" && textBoxX2.Text != "")
                {
                    ResourceWriter writer = new ResourceWriter("project_assess.resources");
                    writer.AddResource("check", "1");
                    writer.AddResource("user", textBoxX1.Text);
                    writer.AddResource("password", textBoxX2.Text);
                    writer.Close();
                }
            }
            else
            {
                textBoxX1.Text = "";
                textBoxX2.Text = "";
                File.Delete("project_assess.resources");
            }

        }
    }
}
